﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Kaixin001.Common;
using System.Text.RegularExpressions;
using Kaixin001.StringUtil;

namespace Kaixin001.ProxyUtil
{
    public class ProxyHelper
    {
        private static string proxyUrl = "http://www.daili.name/";
        private static string listStartHtml = "<td>Last Test最后测试</td></tr>";
        private static string listEndHtml = "<a name='whoToProxy'></a>";

        public ProxyHelper()
        { }

        private static DataTable InitDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("IP");
            dt.Columns.Add("Port");
            dt.Columns.Add("Area");
            dt.Columns.Add("Type");
            dt.Columns.Add("TestTime");

            return dt;
        }

        /// <summary>
        /// 获取代理列表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetProxyList()
        {
            try
            {
                DataTable dt = InitDataTable();

                BaseHttp baseHttp = new BaseHttp();
                string result = baseHttp.GetHtml(baseHttp.doGet(proxyUrl), Encoding.UTF8);

                if (result.Contains("代理之名"))
                {
                    result = StringHelper.GetMidString(result, listStartHtml, listEndHtml);
                    result = DelHtmlString(result).Trim().Replace("验证", ";").Replace(",;,,,,,", ";").Replace(",,", ",");
                    if (result.StartsWith(","))
                    {
                        result = result.Substring(1);
                    }
                    if (result.EndsWith(";"))
                    {
                        result = result.Substring(0, result.Length - 1);
                    }
                    string[] results = result.Split(';');

                    foreach (string str in results)
                    {
                        dt.Rows.Add(str.Split(','));
                    }
                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 取出Html代码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string DelHtmlString(string str)
        {
            string[] Regexs =
                                {
                                    @"<script[^>]*?>.*?</script>",
                                    @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
                                    @"([\r\n])[\s]+",
                                    @"&(quot|#34);",
                                    @"&(amp|#38);",
                                    @"&(lt|#60);",
                                    @"&(gt|#62);",
                                    @"&(nbsp|#160);",
                                    @"&(iexcl|#161);",
                                    @"&(cent|#162);",
                                    @"&(pound|#163);",
                                    @"&(copy|#169);",
                                    @"&#(\d+);",
                                    @"-->",
                                    @"<!--.*\n"
                                };

            string[] Replaces =
                                {
                                    "",
                                    ",",
                                    "",
                                    "\"",
                                    "&",
                                    "<",
                                    ">",
                                    " ",
                                    "\xa1", //chr(161),
                                    "\xa2", //chr(162),
                                    "\xa3", //chr(163),
                                    "\xa9", //chr(169),
                                    "",
                                    "\r\n",
                                    ""
                                };

            string s = str;
            for (int i = 0; i < Regexs.Length; i++)
            {
                s = new Regex(Regexs[i], RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(s, Replaces[i]);
            }
            s.Replace("<", "");
            s.Replace(">", "");
            s.Replace("\r\n", "");
            return s;

        }

    }
}
